﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechaShark_windows_
{
    class Renderer
    {
        //-------------Members-------------
        public BasicEffect currentEffect;

        //-------------Methods-------------
        //constructor
        public Renderer(GraphicsDeviceManager graphics)
        {
            currentEffect = new BasicEffect(graphics.GraphicsDevice);
            //currentEffect.EnableDefaultLighting(); //includes lighting
        }
        //shaders
        #region Shaders
        public void DrawWithSkin(Model model, Matrix[] bones, Matrix world, Matrix view, Matrix proj)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (SkinnedEffect effect in mesh.Effects)
                {
                    effect.SetBoneTransforms(bones);

                    effect.World = world;
                    effect.View = view;
                    effect.Projection = proj;

                    effect.EnableDefaultLighting();

                    effect.SpecularColor = new Vector3(0.25f);
                    effect.SpecularPower = 16;
                }

                mesh.Draw();
            }
        }
        public void DrawWithYellowEffect(Model model, Matrix world, Matrix view, Matrix proj, bool IsTextured)
        {
            currentEffect.TextureEnabled = IsTextured;

            currentEffect.World = world;
            currentEffect.View = view;
            currentEffect.Projection = proj;

            currentEffect.LightingEnabled = true;
            currentEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);//adjust diffuse color
            currentEffect.SpecularColor = new Vector3(0.65f, 0.8f, 0.1f);//adjust specular color
            currentEffect.SpecularPower = 5.0f;//adjust specular brightness
            currentEffect.AmbientLightColor = new Vector3(0.4f, 0.5f, 0.2f); //adjust ambient light color

            currentEffect.DirectionalLight0.Enabled = true;
            currentEffect.DirectionalLight0.DiffuseColor = Vector3.One;
            currentEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1.0f, 1.0f, -1.0f));
            currentEffect.DirectionalLight0.SpecularColor = Vector3.One;
            currentEffect.DirectionalLight1.Enabled = true;
            currentEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            currentEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
            currentEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart parts in mesh.MeshParts)
                    parts.Effect = currentEffect;
                mesh.Draw();
            }
        }
        public void DrawWithDiffuse(Model model, Texture2D diffuseMap, Matrix world, Matrix view, Matrix proj)
        {
            /*
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart parts in mesh.MeshParts)
                {
                    Matrix worldInverseTransposeMatrix = Matrix.Transpose(Matrix.Invert(mesh.ParentBone.Transform * world));
                    currentEffect.Parameters["WorldInverseTransposeMatrix"].SetValue(worldInverseTransposeMatrix);
                }
                mesh.Draw();
            }
             * */
        }
        public void DrawWithGrayEffect(Model model, Matrix world, Matrix view, Matrix proj)
        {
            currentEffect.TextureEnabled = false;
            currentEffect.World = world;
            currentEffect.View = view;
            currentEffect.Projection = proj;

            currentEffect.LightingEnabled = true;
            currentEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);//adjust diffuse color
            currentEffect.SpecularColor = new Vector3(0.1f, 0.1f, 0.1f);//adjust specular color
            currentEffect.SpecularPower = 3.0f;//adjust specular brightness
            currentEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f); //adjust ambient light color

            currentEffect.DirectionalLight0.Enabled = true;
            currentEffect.DirectionalLight0.DiffuseColor = Vector3.One;
            currentEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1.0f, 1.0f, -1.0f));
            currentEffect.DirectionalLight0.SpecularColor = Vector3.One;
            currentEffect.DirectionalLight1.Enabled = true;
            currentEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            currentEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
            currentEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);
            foreach (ModelMesh meshes in model.Meshes)
            {
                foreach (ModelMeshPart parts in meshes.MeshParts)
                    parts.Effect = currentEffect;
                meshes.Draw();
            }
        }
        public void DrawWithHurtTexture(Model model, Texture2D texture, Matrix world, Matrix view, Matrix proj)
        {
            currentEffect.TextureEnabled = true;
            currentEffect.Texture = texture;
            currentEffect.World = world;
            currentEffect.View = view;
            currentEffect.Projection = proj;

            currentEffect.LightingEnabled = true;
            currentEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);//adjust diffuse color
            currentEffect.SpecularColor = new Vector3(0.6f, 0.2f, 0.2f);//adjust specular color
            currentEffect.SpecularPower = 5.0f;//adjust specular brightness
            currentEffect.AmbientLightColor = new Vector3(0.9f, 0.5f, 0.5f); //adjust ambient light color

            currentEffect.DirectionalLight0.Enabled = true;
            currentEffect.DirectionalLight0.DiffuseColor = Vector3.One;
            currentEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1.0f, 1.0f, -1.0f));
            currentEffect.DirectionalLight0.SpecularColor = Vector3.One;
            currentEffect.DirectionalLight1.Enabled = true;
            currentEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            currentEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
            currentEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);
            foreach (ModelMesh meshes in model.Meshes)
            {
                foreach (ModelMeshPart parts in meshes.MeshParts)
                    parts.Effect = currentEffect;
                meshes.Draw();
            }
        }
        public void DrawWithTexture(Model model, Texture2D texture, Matrix world, Matrix view, Matrix proj) 
        {
            currentEffect.TextureEnabled = true;
            currentEffect.Texture = texture;
            currentEffect.World = world;
            currentEffect.View = view;
            currentEffect.Projection = proj;

            currentEffect.LightingEnabled = true;
            currentEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);//adjust diffuse color
            currentEffect.SpecularColor = new Vector3(0.2f, 0.2f, 0.2f);//adjust specular color
            currentEffect.SpecularPower = 5.0f;//adjust specular brightness
            currentEffect.AmbientLightColor = new Vector3(0.5f, 0.5f, 0.5f); //adjust ambient light color

            currentEffect.DirectionalLight0.Enabled = true;
            currentEffect.DirectionalLight0.DiffuseColor = Vector3.One;
            currentEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1.0f, 1.0f, -1.0f));
            currentEffect.DirectionalLight0.SpecularColor = Vector3.One;
            currentEffect.DirectionalLight1.Enabled = true;
            currentEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            currentEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
            currentEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);
            foreach (ModelMesh meshes in model.Meshes)
            {
                foreach (ModelMeshPart parts in meshes.MeshParts)
                    parts.Effect = currentEffect;
                meshes.Draw();
            }
        }

        #endregion
    }
}
